package com.yxinsur.imc.quartz.task;

import com.alibaba.fastjson.JSON;
import com.yxinsur.imc.business.domain.ImcFund;
import com.yxinsur.imc.business.domain.ImcReportData;
import com.yxinsur.imc.business.enums.IsDeletedEnum;
import com.yxinsur.imc.business.enums.ReportTypeEnum;
import com.yxinsur.imc.business.mapper.ImcFundMapper;
import com.yxinsur.imc.business.mapper.ImcOrderMapper;
import com.yxinsur.imc.business.mapper.ImcReportDataMapper;
import com.yxinsur.imc.business.vo.ReportAchievementVo;
import com.yxinsur.imc.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 个人业绩报表定时任务调度
 * 
 * @author imc
 */
@Component("achievementReportTask")
public class AchievementReportTask
{
    private static final Logger logger = LoggerFactory.getLogger(AchievementReportTask.class);

    @Autowired
    private ImcFundMapper imcFundMapper;

    @Autowired
    private ImcOrderMapper imcOrderMapper;

    @Autowired
    private ImcReportDataMapper imcReportDataMapper;

    @Transactional
    public void achievementReportMultipleParams(Long fundId)
    {
        if(fundId != null){
            ImcFund fund = this.imcFundMapper.selectById(fundId);
            if(fund != null){
                String month = DateUtils.dateTimeNow(DateUtils.YYYY_MM);
                Date date = DateUtils.addMonths(DateUtils.parseDate(month +"-" +fund.getApplyEndDate()),fund.getApplyWithSameMonth().intValue());

                Date startDate1 = DateUtils.addMonths(date,-1);
                Date endDate1   = date;
                Date startDate2 = DateUtils.addMonths(startDate1,-1);
                Date endDate2   = DateUtils.addMonths(endDate1,-1);
                Date startDate3 = DateUtils.addMonths(startDate2,-1);
                Date endDate3   = DateUtils.addMonths(endDate2,-1);
                String yearMonth = DateUtils.dateTimeNow(DateUtils.YYYY)+"-12";
                Date yearEndDate = DateUtils.addMonths(DateUtils.parseDate(yearMonth +"-" + fund.getApplyEndDate()),fund.getApplyWithSameMonth().intValue());
                Date yearStartDate = DateUtils.addMonths(yearEndDate,-12);

                List<ReportAchievementVo> reportData = this.imcOrderMapper.selectAchievementReportList(startDate1,endDate1,startDate2,endDate2,startDate3,endDate3,yearStartDate,yearEndDate,fund.getTenantId());
                String username = "system";
                Long tenantId = fund.getTenantId();
                Date nowDate = DateUtils.getNowDate();
                //保存数据
                for (ReportAchievementVo vo:reportData) {
                   ImcReportData data = new ImcReportData();
                   data.setReportType(ReportTypeEnum.ACHIEVEMENT.getCode());
                   data.setReportData(JSON.toJSONString(vo));
                   data.setCreateId(username);
                   data.setCreateTime(nowDate);
                   data.setIsDeleted(IsDeletedEnum.N.getCode());
                   data.setTenantId(tenantId);
                   this.imcReportDataMapper.insert(data);
                }
            }else{
                logger.error("个人业绩报表定时任务调度失败，参数fundId无效");
            }
        }else{
            logger.error("个人业绩报表定时任务调度失败，必传参数fundId为空");
        }
    }
}
